package com.dwxt.community.service;

import com.dwxt.common.base.*;
import com.dwxt.common.util.*;
import com.dwxt.community.dao.*;
import com.dwxt.community.entity.*;
import com.dwxt.community.entity.vo.*;
import com.github.pagehelper.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

import java.util.*;

@Service
public class AdService {
    @Autowired
    private AdMapper adMapper;
    @Autowired
    private AdTypeMapper adTypeMapper;
    @Autowired
    private AdLocationMapper adLocationMapper;

    /**
     *     广告列表 or 搜索
     *
     * @param pageNum   当前页
     * @param pageSize  最大条数
     * @param name      关键字
     * @param type      广告类型
     */
    public BaseResult get(Integer pageNum, Integer pageSize,String name, Integer type) {
        PageHelper.startPage(pageNum, pageSize);
        return BaseResult.ok(new PageInfo<AdVo>(adMapper.selectAll(name,type)));
    }

    /**
     * 添加广告
     *
     * @param ad
     * @return
     */
    public BaseResult insert(Ad ad) {
        Date currDate = new Date();
        ad.setCreateTime(currDate);
        ad.setUpdateTime(currDate);
        ad.setId(IDUtils.genItemId());
        return BaseResult.ok(adMapper.insertSelective(ad));
    }

    /**
     * 删除广告
     *
     * @param id
     * @return
     */
    public BaseResult del(String id) {
        return BaseResult.ok(adMapper.deleteByPrimaryKey(id));
    }

    /**
     * 修改广告
     *
     * @param ad
     * @return
     */
    public BaseResult update(Ad ad) {
        return BaseResult.build(adMapper.updateByPrimaryKey(ad) > 0 ? StatusCode.SUCCESS : StatusCode.FAIL);
    }

    /**
     * 广告位置列表
     * @return
     */
    public BaseResult getAdLocation() {
        return BaseResult.ok(adLocationMapper.selectByExample(null));
    }
    /**
     * 广告类型列表
     * @return
     */
    public BaseResult getAdType() {
        return BaseResult.ok(adTypeMapper.selectByExample(null));
    }
}
